#!/bin/bash
#
# rpcidmapd     Start up and shut down RPC name to UID/GID mapper
#
# Authors:	Chuck Lever <cel@netapp.com>
#
# chkconfig: 345 18 85
# description: Starts user-level daemon for NFSv4 that maps user \
#              names to UID and GID numbers.

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
if [ ! -f /etc/sysconfig/network ]; then
    exit 6
fi
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 6

[ ! -x /usr/sbin/rpc.idmapd ] && exit 5

# Check for and source configuration file otherwise set defaults
[ -f /etc/sysconfig/nfs ] && . /etc/sysconfig/nfs

RETVAL=0
LOCKFILE=/var/lock/subsys/rpcidmapd
prog="rpc.idmapd"

case "$1" in
  start|condstart)
	# Make sure the daemon is not already running.
	[ "$1" = "condstart" -a -n "`pidofproc $prog`" ] && {
		killproc $prog "-SIGHUP" > /dev/null
		exit 0
	}
	rm -f $LOCKFILE

	echo -n $"Starting RPC idmapd: "

	# Make sure the rpc_pipefs filesystem is available
	[ "${RPCMTAB}" != "noload" ] && {
		RPCMTAB=`grep -v '^#' /proc/mounts | \
			awk '{ if ($3 ~ /^rpc_pipefs$/ ) print $2}'`
		[ -z "${RPCMTAB}" ] && {
			[ -x /sbin/lsmod -a -x /sbin/modprobe ] && {
				if ! /sbin/lsmod | grep sunrpc > /dev/null ; then
				 	/sbin/modprobe sunrpc
				fi
			}
			RPCMTAB=`grep -v '^#' /proc/mounts | \
				awk '{ if ($3 ~ /^rpc_pipefs$/ ) print $2}'`
			[ -z "${RPCMTAB}" ] && { \
				echo "Error: RPC MTAB does not exist."
				exit 6
			}
		}
	}

	# Make sure the mount worked.

	# Start daemon.
	daemon $prog ${RPCIDMAPDARGS}
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch $LOCKFILE
	;;
  stop)
	# Stop daemon.
	echo -n $"Stopping RPC idmapd: "
	killproc $prog
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
	;;
  status)
	status rpc.idmapd
	RETVAL=$?
	;;
  restart|reload)
	$0 stop
	$0 start
	RETVAL=$?
	;;
  condrestart)
	if [ -f $LOCKFILE ]; then
		$0 restart
		RETVAL=$?
	fi
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|condstart|condrestart|status}"
	RETVAL=3
	;;
esac

exit $RETVAL
